Use the text type of jqgrid + mysql to implement a simple custom data model

Source: Internet
Author: User
Tags jqgrid
The following is an example of using the text type of jqgrid + mysql to implement a simple custom data model. sometimes, the speed and security of our work may not be the focus of our consideration in the early stage. the flexibility of data may not... the following is an example of using the text type of jqgrid + mysql to implement a simple custom data model.

Sometimes, we may not focus on speed and security in the early stage. data flexibility may be more important to an immature system, here I use thinkphp + mysql + jqgrid to create a simple custom data model.

I used to write a custom model: php is a user-defined form that does not add fields. However, at that time, it was quite troublesome because it didn't have a good tool like jqgrid, of course, fields are not added this time. The text type of the data is used to add json data to implement a virtual "table". some people should know that, if we use mongodb, it will be more flexible and easy to do, but because it is relatively small, it will be used first.

First of all, the features are as follows: through the configuration file, the key, type, sorting, display, and whether the field is required for dynamic configuration, and so on. for the part type, define it as image. let's take a look at the configuration file, the code is as follows:

Return array (// package name prefix 'pack _ pre' => 'pack _', // product management field configuration/*** key is the field index, and storage field * name is the field display name * required? 1 is 0 not * hidden: whether the list is displayed, 1 Hidden, 0 displayed. this option affects search, but does not affect editing * type: three types * the display sequence of fields is the same as that of Keys */'goodsfield' => array (// id is the system reserved field // product name 'title' => array ('name' => 'commodity name ', 'did' => 0, 'required' => 1, 'type' => 'text ',), // spu 'spu' => array ('name' => 'spu', 'ddd' => 0, 'required' => 1, 'type' => 'text',), // image address 'image' => array ('name' => 'image address', 'ddn' => 0, 'requestred' => 1, 'type' => 'IMG ',), // url 'URL' => array ('name' => 'details address ', 'did' => 0, 'required' => 1, 'type' => 'URL ',),), // the following two mappings are for goodsfield // goods_info api map. if there is no ING, the original field 'goodsinfomap' => array (), // pack post api map, the original field 'map' => array (),) is used when the product package sends the amc field ING without ING (),);

You can easily modify the configuration and add, delete, and modify the configuration.

2. the jqgrid code of html is as follows:

JQuery ("# goodsList"). jqGrid ({url: '/index. php? 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: 'lasttime', 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", editur L: '/index. php? M = Home & a = success, // add the MODIFY operation url autowidth: true, height: '20140901 '});


3. the code for the three important functions is as follows:

// Obtain the field name function feild_name ($ feild) {$ fields = C ($ feild); $ feild = ''; foreach ($ fields as $ val) {$ feild. = "'". $ val ['name']. "'," ;}$ feild = rtrim ($ feild,', '); return $ feild;} // Obtain the 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 ['den den ']) {$ 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;} // Obtain the 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 [] = ''. $ row [$ key]. ''; break; case 'IMG ': $ array [] =''; break; default: $ array [] = $ row [$ key];} return $ array ;}

There is nothing in the code, that is, dynamic configuration through the json object.


Tutorial URL:

You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.

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.