Implement a simple custom data model with jqgrid+ and MySQL text type

Source: Internet
Author: User
Tags json mysql text jqgrid
Sometimes we do things that may speed and safety in the early stage is not the focus of our consideration, the flexibility of data may be more important for an immature system, here I use Thinkphp+mysql+jqgrid to make a simple custom data model. A brief summary.
I've also written a custom model before: PHP A custom form does not add fields, but because there is no jqgrid this more useful tools, so at that time is still more trouble, this time is not adding fields, the use of the text type of data plus JSON data, the realization of virtual "table." Heard here, some students should know, if we use MongoDB actually do it more flexible and simple. But, because this thing is relatively small, this is used first.
As shown in figure:
First functional features
Through the configuration file, dynamically Configure the field key, type, sort, show, whether required to fill in and so on
such as image type, defined as image can,

Look at the configuration file

The code is as follows Copy Code

<?php
Return Array (
Package name Prefix
' Pack_pre ' => ' Pack_ ',
Product Management field Configuration
/**
* Key is field index, storage field
* Name for field display name
* Whether the required must fill 1 is 0 not
* Hidden: List is displayed, 1 hidden, 0 display, this option will affect the search, but does not affect the editing
* Type: three types
* Field display order is the same as key order
*/
' Goodsfield ' => Array (
ID for system reserved field
Product Name
' Title ' =>array (
' Name ' => ' Commodity name ',
' Hidden ' =>0,
' Required ' =>1,
' Type ' => ' text ',
),
Spu
' SPU ' =>array (
' Name ' => ' SPU ',
' Hidden ' =>0,
' Required ' =>1,
' Type ' => ' text ',
),
Picture Address
' Image ' =>array (
' Name ' => ' picture address ',
' Hidden ' =>0,
' Required ' =>1,
' Type ' => ' img ',
),
Url
' URL ' =>array (
' Name ' => ' details address ',
' Hidden ' =>0,
' Required ' =>1,
' type ' => ' url ',
),

),
The following two mappings are for Goodsfield
Goods_info API map, no mapping that uses the original field
' Goodsinfomap ' =>array (

),
Pack Post API map, commodity package AMC Field Map No mapping that uses the original field
' Map ' =>array (

),
);


Can be configured, very convenient to modify, and a change of all changes, additions and deletions to change.
2,html's Jqgrid code

The code is as follows Copy Code
JQuery ("#goodsList"). Jqgrid ({
URL: ' __app__?m=home&a=getlist ',
DataType: "JSON",
Mtype: ' POST ',
colnames:[' ID ', <{$feildname}>, ' Modify time ', ' Operation '],
colmodel:[
{name: ' id ', Width:25,index: ' id ', setgridheight: ' Auto '},
<{$feildmap}>
{name: ' Lastdotime ', sortable:true},
{name: ' edit ', search:false,sortable:false},
],
Rownum:10,
ROWLIST:[10,20,30],
Multiselect:true,//check box
Pager: ' #goodsPager ',
Sortname: ' Lastdotime ',
Viewrecords:true,
SortOrder: "Desc",
Editurl: ' __app__?m=home&a=oper ',//Add Modify action URL
Autowidth:true,
Height: ' 100% '
});

3, three more important functions

The code is as follows Copy Code
<?php
Get field Name
function Feild_name ($feild) {
$fields = C ($feild);
$feild = ';
foreach ($fields as $val) {
$feild. = "'. $val [' name ']." ', ';
}
$feild = RTrim ($feild, ', ');
return $feild;
}
Get Field Dictionary
function Feild_map ($feild) {
$fields = C ($feild);
$feildmap = ';
foreach ($fields as $key => $val) {
$feildmap. = ' {name:\ '. $key. ', Sortable:false,editable:true ';
if (1== $val [' hidden ']) {
$feildmap. = ', hidedlg:true,hidden:true,editrules:{edithidden:true, ';
}else{
$feildmap. = ', editrules:{';
}
if (1== $val [' required ']) {
$feildmap. = ' Required:true} ';
}else{
$feildmap. = ' Required:false} ';
}
$feildmap. = '}, '. ' \ n ";
}
return $feildmap;
}
Get field List
function Feild_list ($feild, $array =array (), $row =array ()) {
$row = Json_decode ($row [' Value '],true);
$fields = C ($feild);
foreach ($fields as $key => $val) {
Switch ($val [' type ']) {
Case ' URL ': $array []= ' <a href= '. $row [$key]. ' style= ' color: #0018D1 "target=" _blank ">". $row [$key]. ' </a> ';
Case ' img ': $array []= ' ';
Default: $array [] = $row [$key];
}
}
return $array;
}

There is nothing in the code that is dynamically configured through JSON objects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.